import React from "react";
import {observer, inject} from 'mobx-react';
import * as mobx from 'mobx';
import {withRouter} from 'react-router-dom';
import {Tabs, Tools} from "weaver-mobile";
import CertInfo from "./CertInfo";
import TamperInfo from "./TamperInfo";
import * as authTypes from "../../../constants/secondAuthType";
import {suppoertCAAuth} from "../../../util/CAJsApi";

import "./style/index.less";

const getLabel = Tools.getLabel;

@withRouter
@inject("secondAuthDetail")
@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
    }

    componentWillMount() {
        const {secondAuthDetail, location} = this.props;
        const urlParams = (location && location.state) || {};
        const logid = urlParams.logid || "-1";
        secondAuthDetail.loadAuthDetail(logid);
    }

    switchTab(tab) {
        const {secondAuthDetail} = this.props;
        secondAuthDetail.changeTab(tab.tabKey);
    }

    render() {
        const {secondAuthDetail} = this.props;
        const {tabs, selectedKey, tamperInfo, certInfo, protectType} = secondAuthDetail;
        const tabDatas = mobx.toJS(tabs);

        let tamperData = mobx.toJS(tamperInfo);
        let certData = mobx.toJS(certInfo);

        const {checkResult = "0"} = tamperData;

        let selectedIndex = 0;
        const childComp = [];

        if (certData.length > 0&& ((suppoertCAAuth() && protectType == authTypes.CA_AUTH) || protectType == authTypes.QYS)) {       //支持CA认证，e-mobile端打开，会显示2个tab页
            selectedIndex = selectedKey == "certInfo" ? 0 : 1;
            childComp.push(<CertInfo data={[...certData]}/>);
            childComp.push(<TamperInfo {...tamperData}/>);
        } else {     //仅显示篡改信息的tab页
            selectedIndex = 0;
            childComp.push(<TamperInfo {...tamperData}/>);
        }

        const tabProps = {
            initialPage: selectedIndex,
            onChange: this.switchTab.bind(this)
        };

        if (checkResult == "9") {       //契约锁二次认证
            return <CertInfo data={[...certData]}/>
        } else {        //数据保护(CA或者契约锁）
            if (tabDatas && tabDatas.length >= selectedIndex) {
                return (
                    <Tabs {...tabProps} tabs={tabDatas} swipeable={false}>
                        {childComp}
                    </Tabs>
                );
            } else {
                return <div></div>
            }
        }
    }
}